#include <openssl/evp.h>
#include <openssl/err.h>
#include "sm3.h"
#include <stdio.h>
#include <stdlib.h>

void sm3_hash(const unsigned char *data, size_t data_len, unsigned char output[32]) {
    EVP_MD_CTX *mdctx = EVP_MD_CTX_new();
    EVP_DigestInit_ex(mdctx, EVP_sm3(), NULL);
    EVP_DigestUpdate(mdctx, data, data_len);
    EVP_DigestFinal_ex(mdctx, output, NULL);
    EVP_MD_CTX_free(mdctx);
}

void sm3_hmac(const unsigned char *data, size_t data_len, const unsigned char *key, size_t key_len, unsigned char output[32]) {
    EVP_MAC_CTX *mac_ctx = EVP_MAC_CTX_new(id);
    if (mac_ctx == NULL) {
        ERR_print_errors_fp(stderr);
        return;
    }

    if (EVP_MAC_init(mac_ctx, key, key_len, NULL) <= 0) {
        EVP_MAC_CTX_free(mac_ctx);
        ERR_print_errors_fp(stderr);
        return;
    }

    if (EVP_MAC_update(mac_ctx, data, data_len) <= 0) {
        EVP_MAC_CTX_free(mac_ctx);
        ERR_print_errors_fp(stderr);
        return;
    }

    if (EVP_MAC_final(mac_ctx, output, NULL, 0) <= 0) {
        EVP_MAC_CTX_free(mac_ctx);
        ERR_print_errors_fp(stderr);
        return;
    }

    EVP_MAC_CTX_free(mac_ctx);
}
